package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constans.message.MessageConstants;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {


    @Autowired
    ApBehaviorService apBehaviorService;
    /**
     * APP点赞行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //检查参数
        if (dto==null||dto.getArticleId()==null||dto.getType()==null||dto.getOperation()==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser apUser = ApUserThreadLocalUtils.getUser();

        if (dto.getEquipmentId()==null){
            if (apUser==null){
                ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }else {
                if (apUser.getId()==0){ //0表示游客登录
                    dto.setEquipmentId(Long.valueOf(apUser.getPhone()));
                }else {
                    dto.setEquipmentId(apUser.getId().longValue());
                }
            }
        }
        //查询数据库
        ApBehaviorEntry apBehaviorEntry = apBehaviorService.getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId()));
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
           /*  apBehaviorEntry=new ApBehaviorEntry();
            apBehaviorEntry.setEntryId(dto.getEquipmentId().intValue());
            apBehaviorEntry.setType(dto.getType());
            apBehaviorEntry.setCreatedTime(new Date());
            apBehaviorService.save(apBehaviorEntry);*/
        }
        ApLikesBehavior likesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getArticleId, dto.getArticleId()).eq(ApLikesBehavior::getEntryId, dto.getEquipmentId()));
        if (likesBehavior==null){
            likesBehavior=new ApLikesBehavior();
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setCreatedTime(new Date());
            likesBehavior.setEntryId(dto.getEquipmentId().intValue());
        }
        likesBehavior.setType(dto.getType());
        likesBehavior.setOperation(dto.getOperation());
        //保存数据
        saveOrUpdate(likesBehavior);
        sendmessage(dto,1);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    KafkaTemplate kafkaTemplate;
    //发送消息进行实时流更新
    private void sendmessage(LikesBehaviorDto dto, int i) {
        UpdateArticleMess mess=new UpdateArticleMess();
        mess.setAdd(i);
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        kafkaTemplate.send(MessageConstants.HOT_ARTICLE_BEHAVIOR_SEND_TOPIC, JSON.toJSONString(mess));
    }
}
